﻿using System;
using System.Linq;
using System.Web.Services;
using System.Web.UI;
using Dost.Khcn.Biz;
using Dost.Khcn.Model;
using Dost.Khcn.Web.Common;

namespace Dost.Khcn.Web.Admin
{
    public partial class _Default : Page
    {
        #region Properites

        protected bool IsAdvanceSearch {
            get { return WebCommon.CurrrentEmployee != null; }
        }

        private static LinhVucBiz _linhVucBiz;
        private static LinhVucBiz LinhVucManager
        {
            get { return _linhVucBiz ?? (_linhVucBiz = new LinhVucBiz()); }
        }

        private static ChuyenNganhBiz _chuyenNganhBiz;
        private static ChuyenNganhBiz ChuyenNganhManager
        {
            get { return _chuyenNganhBiz ?? (_chuyenNganhBiz = new ChuyenNganhBiz()); }
        }

        private static HocHamBiz _hocHamBiz;
        private static HocHamBiz HocHamManager
        {
            get { return _hocHamBiz ?? (_hocHamBiz = new HocHamBiz()); }
        }

        private static HocViBiz _hocViBiz;
        private static HocViBiz HocViManager
        {
            get { return _hocViBiz ?? (_hocViBiz = new HocViBiz()); }
        }

        private static ChuyenGiaOnlineBiz _chuyenGiaBiz;
        private static ChuyenGiaOnlineBiz ChuyenGiaManager
        {
            get { return _chuyenGiaBiz ?? (_chuyenGiaBiz = new ChuyenGiaOnlineBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region WebMethods

        [WebMethod(EnableSession = true)]
        public static object SearchChuyenGiaSimple(int jtStartIndex, int jtPageSize, string jtSorting, SearchChuyenGiaSimpleModel searchModel)
        {
            if (string.IsNullOrWhiteSpace(searchModel.HoTen)
                && string.IsNullOrWhiteSpace(searchModel.DiaChiDonvi)
                && string.IsNullOrWhiteSpace(searchModel.TenDonVi)
                && searchModel.LinhVucId <= 0
                && searchModel.NhomLinhVucId <= 0)
            {
                return new { Result = "OK", Records = new { }, TotalRecordCount = 0 };
            }

            if (jtPageSize > 25) jtPageSize = 50;

            var totalRecords = ChuyenGiaManager.SearchChuyenGiaSimpleCount(searchModel);

            var results = ChuyenGiaManager.SearchChuyenGiaSimple(jtStartIndex, jtPageSize, jtSorting, searchModel);

            return new { Result = "OK", Records = results, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object SearchChuyenGiaAdvance(int jtStartIndex, int jtPageSize, string jtSorting, SearchChuyenGiaAdvanceModel searchModel)
        {
            if (WebCommon.CurrrentEmployee == null)
            {
                return new {Result = "ERROR", Message = "SessionTimeout"};
            }

            if (string.IsNullOrWhiteSpace(searchModel.HoTen)
                && string.IsNullOrWhiteSpace(searchModel.DiaChiDonvi)
                && string.IsNullOrWhiteSpace(searchModel.TenDonVi)
                && searchModel.LinhVucId <= 0
                && searchModel.NhomLinhVucId <= 0
                && string.IsNullOrWhiteSpace(searchModel.TenLuanAn)
                && string.IsNullOrWhiteSpace(searchModel.TenCongTrinh))
            {
                return new { Result = "OK", Records = new { }, TotalRecordCount = 0 };
            }

            if (jtPageSize > 25) jtPageSize = 50;

            var totalRecords = ChuyenGiaManager.SearchChuyenGiaAdvanceCount(searchModel);

            var results = ChuyenGiaManager.SearchChuyenGiaAdvance(jtStartIndex, jtPageSize, jtSorting, searchModel);
            foreach (var result in results)
            {
                if (!string.IsNullOrWhiteSpace(result.CongTrinh))
                {
                    result.CongTrinh = "- " + result.CongTrinh.Replace("|", "<br />- ");
                }
            }

            return new { Result = "OK", Records = results, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object GetAllNhomLinhVucs()
        {
            var nhomLinhvucs = LinhVucManager.GetAllNhomLinhVucs();
            nhomLinhvucs.Insert(0, new Data.EntityModel.NhomLinhVuc { Id = 0, Ten = string.Empty });

            return nhomLinhvucs.Select(x => new { x.Id, x.MaTen });
        }

        [WebMethod(EnableSession = true)]
        public static object GetAllLinhVucs()
        {
            var linhVucs = LinhVucManager.GetAllLinhVucs();
            linhVucs.Insert(0, new Data.EntityModel.LinhVuc { Id = 0, Ten = string.Empty });
            return linhVucs;
        }

        [WebMethod(EnableSession = true)]
        public static object GetAllHocHams()
        {
            var hocHams = HocHamManager.GetAllHocHams();
            hocHams.Insert(0, new Data.EntityModel.HocHam { Id = 0, Ten = string.Empty });
            return hocHams;
        }

        [WebMethod(EnableSession = true)]
        public static object GetAllHocVis()
        {
            var hocVis = HocViManager.GetAllHocVis();
            hocVis.Insert(0, new Data.EntityModel.HocVi { Id = 0, Ten = string.Empty });
            return hocVis;
        }

        [WebMethod(EnableSession = true)]
        public static object GetAllChuyenNganhs()
        {
            var chuyenNganhs = ChuyenNganhManager.GetAllChuyenNganhs();
            chuyenNganhs.Insert(0, new Data.EntityModel.ChuyenNganh { Id = 0, Ten = string.Empty });
            return chuyenNganhs;
        }

        #endregion
    }
}